package com.infore.airsupervise.adapter;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.infore.airsupervise.R;
import com.infore.airsupervise.utils.ImageUtil;

import java.io.Serializable;
import java.util.List;

import cn.finalteam.galleryfinal.PhotoPreviewActivity;
import cn.finalteam.galleryfinal.model.PhotoInfo;

/**
 * 照片展示的适配器
 *
 * @author: dingxinlin
 * @date: 2016-10-19 16:33
 */
public class PhotoAdapter extends RecyclerView.Adapter<PhotoAdapter.ViewHolder> {

    private int itemWidth;
    private List<PhotoInfo> datas;

    public PhotoAdapter(List<PhotoInfo> datas, int itemWidth) {
        this.itemWidth = itemWidth;
        this.datas = datas;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_photo, parent, false);
        ViewGroup.LayoutParams lp = view.getLayoutParams();
        lp.width = itemWidth;
        lp.height = itemWidth;
        view.setLayoutParams(lp);

        ViewHolder vh = new ViewHolder(view);

        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        PhotoInfo info = datas.get(position);
        String url = info.getPhotoPath();
        ImageUtil.getInstance().displayImage(holder.imageView, url,itemWidth, itemWidth);

        // item点击
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(view.getContext(), PhotoPreviewActivity.class);
                intent.putExtra("photo_list", (Serializable) datas);
                view.getContext().startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas==null ? 0 : datas.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        public ViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.item_image_view);
        }
    }
}
